package com.escool.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.escool.model.TbPerfilPago;
import com.escool.service.PerfilPagoService;

@Controller
public class PerfilPagoController {
	
	@Autowired
	PerfilPagoService perfilPagoService;
	
	@RequestMapping("showPerfilPago")
	public String showPerfilPago(Map<String, Object> map){
		map.put("perfil", new TbPerfilPago());
		map.put("perfilPagoList", perfilPagoService.listPerfPago());
		return "perfilPago";
	}
	
	@RequestMapping("addPerfilPago")
	public String addPerfilPago(@ModelAttribute("perfil") @Valid TbPerfilPago tbPerfilPago, BindingResult  result){
		try{
			if(!result.hasErrors()){
				perfilPagoService.addPerfPago(tbPerfilPago);
				
			}
			return "redirect:/showPerfilPago";
			
		}catch(Exception ex){
			return "login";
		}
		
	}
	
	@RequestMapping("editPerfilPago")
	public String editPerfilPago(@ModelAttribute("perfil") @Valid TbPerfilPago tbPerfilPago, BindingResult  result){
		try{
			if(!result.hasErrors()){
				perfilPagoService.updatePerfPago(tbPerfilPago);
				
			}
			return "redirect:/showPerfilPago";
			
		}catch(Exception ex){
			return "login";
		}
		
	}
	
	@RequestMapping("delPerfilPago")
	public String delPerfilPago(@ModelAttribute("perfil") @Valid TbPerfilPago tbPerfilPago, BindingResult  result){
		try{
			if(!result.hasErrors()){
				perfilPagoService.deletePerfPago(tbPerfilPago.getIdPerfilPago());
				
			}
			return "redirect:/showPerfilPago";
			
		}catch(Exception ex){
			return "login";
		}
		
	}

}
